package com.pay.ioopos.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import com.pay.ioopos.KeyInfo;
import com.pay.ioopos.R;
import com.pay.ioopos.fragment.TipFragment;
import com.pay.ioopos.fragment.TipFragment.TipType;
import com.pay.ioopos.fragment.UpdateFragment;
import com.pay.ioopos.fragment.UpdateFragment.OnUpdateListener;

import static com.pay.ioopos.KeyInfo.KEY_CANCEL;
import static com.pay.ioopos.KeyInfo.KEY_ENTER;
import static com.pay.ioopos.fragment.TipFragment.TipType.FAIL;
import static com.pay.ioopos.fragment.TipFragment.TipType.SUCCESS;
import static com.pay.ioopos.fragment.TipFragment.TipType.WAIT;
import static com.pay.ioopos.fragment.TipFragment.TipType.WARN;

public class UpdateActivity extends AbstractActivity implements OnUpdateListener {
    private static final String TAG = UpdateActivity.class.getSimpleName();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        UpdateFragment fragment = new UpdateFragment();
        fragment.setListener(this);
        setMainFragment(fragment);
    }

    @Override
    public boolean onKeyDown(KeyInfo keyInfo) {
        TipType type = tipFragment().getType();
        if (type == SUCCESS || type == FAIL) {
            if (keyInfo == KEY_ENTER) {
                Intent intent = new Intent(this, SystemActivity.class);
                startActivity(intent);
            }
        }
        if (type != WAIT && (keyInfo == KEY_ENTER || keyInfo == KEY_CANCEL)) {
            Intent intent = new Intent(this, SystemActivity.class);
            startActivity(intent);
        }
        return false;
    }

    @Override
    public void onChecked(boolean toUpdate) {
        if (!toUpdate) {
            tipFragment().onChange(WARN,"当前版本不需要升级");
        }
    }

    @SuppressLint("DefaultLocale")
    @Override
    public void onDownloading(int totalLength, int loadedLength) {
        tipFragment().onChange(WARN,String.format("正在下载_%d/%d", loadedLength, totalLength));
    }

    @Override
    public void onDownload(String file) {
        tipFragment().onChange(WAIT,"正在安装");
        Intent mIntent = new Intent("com.sanstar.quiet.install");
        mIntent.putExtra("Package_Path",file);
        mIntent.putExtra("Package_Name", packageName());
        mIntent.putExtra("Package_Class", MainActivity.class.getName());
        sendBroadcast(mIntent);
    }

    @Override
    public void onError(String msg) {
        tipFragment().onChange(FAIL, msg);
    }

    private String packageName() {
        try {
            PackageManager packageManager = getPackageManager();
            PackageInfo packageInfo = packageManager.getPackageInfo(getPackageName(), 0);
            return packageInfo.packageName;
        } catch (Exception e) {
            Log.e(TAG, "packageName: ", e);
            onError(e.getMessage());
        }
        return null;
    }

    private TipFragment tipFragment() {
        return ((TipFragment)mainFragment);
    }
}
